package com.qf.fmall.job;

import com.github.wxpay.sdk.WXPay;
import com.qf.fmall.config.PayConfig;
import com.qf.fmall.dao.OrderItemMapper;
import com.qf.fmall.dao.OrdersMapper;
import com.qf.fmall.dao.ProductSkuMapper;
import com.qf.fmall.entity.OrderItem;
import com.qf.fmall.entity.Orders;
import com.qf.fmall.entity.ProductSku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王权富贵
 */
@Component
public class OrderTimeoutCheckJob {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private OrdersMapper ordersMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ProductSkuMapper productSkuMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private OrderItemMapper orderItemMapper;

    private WXPay wxPay = new WXPay(new PayConfig());

    @Scheduled(cron = "0/10 * * * * ?")
    public void checkAndCloseOrder() throws Exception {
        /*
         *   1 ,根据查询 --- status
         * */
        Date date = new Date(System.currentTimeMillis() - 30 * 60 * 1000);
        Example example = new Example(Orders.class);
        example.createCriteria()
                .andEqualTo("status", 1)
                .andLessThan("createTime", date);
        List<Orders> orders = ordersMapper.selectByExample(example);
        for (Orders order : orders) {
            Map<String, String> map = new HashMap<>();
            map.put("out_trade_no", order.getOrderId());
            Map<String, String> resp = wxPay.orderQuery(map);
            String s = resp.get("trade_state");
            System.out.println(s);
            if ("NOTPAY".equalsIgnoreCase(resp.get("trade_state"))) {
                //关闭当前连接
                Map<String, String> resp1 = wxPay.orderQuery(map);
                System.out.println(resp1);
                closeOrder(order.getOrderId());
            } else {
                continue;
            }
        }
    }


    public synchronized void closeOrder(String orderId) {
        System.out.println(orderId);
        Orders orders = new Orders();
        //根据ID进行修改 orders
        orders.setOrderId(orderId);
        orders.setStatus("6"); //已关闭
        orders.setCloseType(1); // 关闭类型，超时未支付
        ordersMapper.updateByPrimaryKeySelective(orders);
        //快照查询
        Example example = new Example(Orders.class);
        example.createCriteria()
                .andEqualTo("orderId", orderId);
        List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
        for (OrderItem orderItem : orderItems) {
            ProductSku productSku = productSkuMapper.selectByPrimaryKey(orderItem.getSkuId());
            productSku.setStock(productSku.getStock() + orderItem.getBuyCounts());
            productSkuMapper.updateByPrimaryKey(productSku);
        }
    }
}
